package com.stone.framework.workflow.definition.handler.assignment;

import java.util.List;
import java.util.Optional;

import com.bstek.uflo.env.Context;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.process.node.TaskNode;
import com.bstek.uflo.process.node.UserData;
import com.stone.framework.core.exception.BusinessException;
import com.stone.framework.workflow.client.UserClient;

public abstract class GenericAssignmentHandler extends AbstractAssignmenthandler {

    private UserClient userClient;

    @Override
    protected String getAssigneeRole(TaskNode taskNode, ProcessInstance processInstance, Context context) {
        List<UserData> datas = taskNode.getUserData();
        String role = datas.stream().filter(userData -> "role".equals(userData.getKey())).findAny()
            .map(userData -> userData.getValue()).orElseThrow(() -> new BusinessException("请配置相关审批角色"));
        return role;
    }

    @Override
    protected String getPromoter(TaskNode taskNode, ProcessInstance processInstance, Context context) {
        return Optional.ofNullable(processInstance.getPromoter()).orElseThrow(() -> new BusinessException("无申请人信息"));
    }

    @Override
    protected List<String> getAssignees(String promoter, String role) {
        return userClient.getByRole(role);
    }

    @Override
    protected void sendMessage(TaskNode taskNode, ProcessInstance processInstance, Context context,
        List<String> assignees) {
        // TODO
    }

    protected void setUserClient(UserClient userClient) {
        this.userClient = userClient;
    }

}
